package homework;


import java.util.Stack;

// class ListNode {
//    int val;
//    ListNode next = null;
//
//    ListNode(int val) {
//        this.val = val;
//    }
//}
public class HomeWoek19 {
    public boolean chkPalindrome(ListNode A) {
        Stack<Integer>stack=new Stack<>();
        ListNode cur=A;
        int count=0;
        while (cur!=null){
            cur=cur.next;
            count++;
        }
        if(count%2==0){
            for (int i = 0; i < count/2; i++) {
                stack.add(A.val);
                A=A.next;
            }
            for (int i = 0; i < count/2; i++) {
                if(stack.peek()==A.val){
                    stack.pop();
                    A=A.next;
                }
            }
        }else {
            for (int i = 0; i < count/2; i++) {
                stack.add(A.val);
                A=A.next;
            }
            A=A.next;
            for (int i = 0; i < count/2; i++) {
                if(stack.peek()==A.val){
                    stack.pop();
                    A=A.next;
                }
            }
        }
        if(stack.empty()){
            return true;
        }else {
            return false;
        }
    }

    public static void main(String[] args) {
        ListNode listNode1=new ListNode(1);
        ListNode listNode2=new ListNode(2);
        ListNode listNode3=new ListNode(2);
        ListNode listNode4=new ListNode(1);
        listNode1.next=listNode2;
        listNode2.next=listNode3;
        listNode3.next=listNode4;
        HomeWoek19 homeWoek19=new HomeWoek19();
        homeWoek19.chkPalindrome(listNode1);
    }
}
